package chat.fragments;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.orm.SugarRecord;
import com.whu.zj.MyApplication;
import com.whu.zj.R;

import java.util.ArrayList;
import java.util.List;

import chat.activities.RoomChatActivity;
import chat.adapter.RoomAdapter;
import chat.network.http.GetAllRooms;
import inter.Delegate;
import inter.ResponseStatus;
import model.Room;

/**
 * 群组界面
 * Created by 凯 on 2015/10/26.
 */
public class RoomFragment extends Fragment {
    ListView lv_room;

    PullToRefreshListView pullToRefreshListView;

    private List<Room> userRooms = new ArrayList<Room>();
    RoomAdapter roomAdapter = new RoomAdapter();
    {
        roomAdapter.setUserRooms(userRooms);
    }

    Delegate getAllRoomsResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            pullToRefreshListView.onRefreshComplete();

            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetAllRooms.ResponseRooms responseRooms = gson.fromJson(response, GetAllRooms.ResponseRooms.class);
            if (responseRooms.getStatus().equals(ResponseStatus.SUCCESS))
            {
                SugarRecord.deleteAll(Room.class);
                for(GetAllRooms.UserRooms um:responseRooms.getUserRooms())
                {
                    Room room = new Room();
                    room.setRoom_ID(um.getRoom_ID());
                    room.setJoin_Role(um.getJoin_Role());
                    room.setRoom_Name(um.getRoom_Name());
                    SugarRecord.save(room);
                }

                getRoomsFromDb(userRooms);
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            pullToRefreshListView.onRefreshComplete();
        }
    };

    GetAllRooms getAllRooms = new GetAllRooms();
    {
        getAllRooms.setDelegate(getAllRoomsResponse);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_group_im, container, false);
        initview(view);
        getRoomsFromDb(userRooms);
        return view;
    }

    // 本地获取数据
    public void getRoomsFromDb(List<Room> rooms)
    {
        rooms.clear();
        List<Room> rms = SugarRecord.listAll(Room.class);
        rooms.addAll(rms);
        roomAdapter.notifyDataSetChanged();
    }

    // 初始化
    private void initview(View view) {
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.id_refreshlistview);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                getAllRooms.get();
            }
        });
        lv_room = pullToRefreshListView.getRefreshableView();
        lv_room.setAdapter(roomAdapter);

        lv_room.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Room userRoom = userRooms.get(position-1);
                Intent intent = new Intent(getActivity(), RoomChatActivity.class);
                intent.putExtra("userRoom",userRoom);
                getActivity().startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllRooms.setUser_ID(MyApplication.getOwnUser().getUser_ID());
        getAllRooms.get();
    }
}
